﻿namespace QLPhongKham.BaseClass.Chung
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDMDangNhap
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _iid;
        private int _iidchuongtrinh;
        private int _iidnhanvien;
        private int _inhomsudung;
        private string _smatkhau;
        private string _squyensudung;
        private string _sSchema;
        private string _stendangnhap;
        private string _stennguoidung;

        public dtoDMDangNhap()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDMDangNhap(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDMDangNhap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._iid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDMDangNhap.cs");
                return false;
            }
        }

        public bool f_kiemtra_quantrihethuong()
        {
            if (this._iid == 0)
            {
                return false;
            }
            int iddangnhap = this._iid;
            return this.f_kiemtra_quantrihethuong(iddangnhap);
        }

        public bool f_kiemtra_quantrihethuong(int iddangnhap)
        {
            this.KhoiTao();
            this._iid = iddangnhap;
            this.Load();
            return (this._inhomsudung == 1);
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDMDangNhap.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDMDangNhap.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDMDangNhap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnTENDANGNHAP, ",", columnMATKHAU, ",", columnNHOMSUDUNG, ",", columnTENNGUOIDUNG, ",", columnQUYENSUDUNG, 
                    ",", columnIDCHUONGTRINH, ",", columnIDNHANVIEN, ") values(", this._iid, ",'", this._stendangnhap, "','", this._smatkhau, "',", this._inhomsudung, ",:", columnTENNGUOIDUNG, ",:", columnQUYENSUDUNG, 
                    ",", this._iidchuongtrinh, ",", this._iidnhanvien, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENNGUOIDUNG, NpgsqlDbType.Text, 200).Value = this._stennguoidung;
                command.Parameters.Add(columnQUYENSUDUNG, NpgsqlDbType.Text, 200).Value = this._squyensudung;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDMDangNhap.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._iid = 0;
            this._stendangnhap = "";
            this._smatkhau = "";
            this._inhomsudung = 0;
            this._stennguoidung = "";
            this._squyensudung = "";
            this._iidchuongtrinh = 0;
            this._iidnhanvien = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDMDangNhap.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                string str = "";
                str = str + ((this._iid == 0) ? "" : string.Concat(new object[] { " and ", columnID, "=", this._iid })) + ((this._stendangnhap == "") ? "" : (" and " + columnTENDANGNHAP + "='" + this._stendangnhap + "'")) + ((this._smatkhau == "") ? "" : (" and " + columnMATKHAU + "='" + this._smatkhau + "'"));
                if (str == "")
                {
                    str = str + " 1=0";
                }
                else
                {
                    str = " 1=1 " + str;
                }
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTENDANGNHAP + "," + columnMATKHAU + "," + columnNHOMSUDUNG + "," + columnTENNGUOIDUNG + "," + columnQUYENSUDUNG + "," + columnIDCHUONGTRINH + "," + columnIDNHANVIEN + " from " + this._sSchema + "." + tableName + " where " + str, connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._iid = int.Parse(dataTable.Rows[0][columnID].ToString());
                    this._stendangnhap = dataTable.Rows[0][columnTENDANGNHAP].ToString();
                    this._smatkhau = dataTable.Rows[0][columnMATKHAU].ToString();
                    this._inhomsudung = int.Parse(dataTable.Rows[0][columnNHOMSUDUNG].ToString());
                    this._stennguoidung = dataTable.Rows[0][columnTENNGUOIDUNG].ToString();
                    this._squyensudung = dataTable.Rows[0][columnQUYENSUDUNG].ToString();
                    this._iidchuongtrinh = int.Parse(dataTable.Rows[0][columnIDCHUONGTRINH].ToString());
                    this._iidnhanvien = int.Parse(dataTable.Rows[0][columnIDNHANVIEN].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDMDangNhap.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDMDangNhap.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTENDANGNHAP + "," + columnMATKHAU + "," + columnNHOMSUDUNG + "," + columnTENNGUOIDUNG + "," + columnQUYENSUDUNG + "," + columnIDCHUONGTRINH + "," + columnIDNHANVIEN + " from " + this._sSchema + "." + tableName + " order by " + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDMDangNhap.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDMDangNhap.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnTENDANGNHAP, " = '", this._stendangnhap, "',", columnMATKHAU, " = '", this._smatkhau, "',", columnNHOMSUDUNG, " = ", this._inhomsudung, 
                    ",", columnTENNGUOIDUNG, " = :", columnTENNGUOIDUNG, ",", columnQUYENSUDUNG, " = :", columnQUYENSUDUNG, ",", columnIDCHUONGTRINH, " = ", this._iidchuongtrinh, ",", columnIDNHANVIEN, " = ", this._iidnhanvien, 
                    " where ", columnID, " = ", this._iid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENNGUOIDUNG, NpgsqlDbType.Text, 200).Value = this._stennguoidung;
                command.Parameters.Add(columnQUYENSUDUNG, NpgsqlDbType.Text, 200).Value = this._squyensudung;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDMDangNhap.cs");
                return false;
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDCHUONGTRINH
        {
            get
            {
                return "idchuongtrinh";
            }
        }

        public static string columnIDNHANVIEN
        {
            get
            {
                return "idnhanvien";
            }
        }

        public static string columnMATKHAU
        {
            get
            {
                return "matkhau";
            }
        }

        public static string columnNHOMSUDUNG
        {
            get
            {
                return "nhomsudung";
            }
        }

        public static string columnQUYENSUDUNG
        {
            get
            {
                return "quyensudung";
            }
        }

        public static string columnTENDANGNHAP
        {
            get
            {
                return "tendangnhap";
            }
        }

        public static string columnTENNGUOIDUNG
        {
            get
            {
                return "tennguoidung";
            }
        }

        public int ID
        {
            get
            {
                return this._iid;
            }
            set
            {
                this._iid = value;
            }
        }

        public int IDCHUONGTRINH
        {
            get
            {
                return this._iidchuongtrinh;
            }
            set
            {
                this._iidchuongtrinh = value;
            }
        }

        public int IDNHANVIEN
        {
            get
            {
                return this._iidnhanvien;
            }
            set
            {
                this._iidnhanvien = value;
            }
        }

        public string MATKHAU
        {
            get
            {
                return this._smatkhau;
            }
            set
            {
                this._smatkhau = value;
            }
        }

        public int NHOMSUDUNG
        {
            get
            {
                return this._inhomsudung;
            }
            set
            {
                this._inhomsudung = value;
            }
        }

        public string QUYENSUDUNG
        {
            get
            {
                return this._squyensudung;
            }
            set
            {
                this._squyensudung = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "dmdangnhap";
            }
        }

        public string TENDANGNHAP
        {
            get
            {
                return this._stendangnhap;
            }
            set
            {
                this._stendangnhap = value;
            }
        }

        public string TENNGUOIDUNG
        {
            get
            {
                return this._stennguoidung;
            }
            set
            {
                this._stennguoidung = value;
            }
        }
    }
}

